<?php

define('CURSCRIPT', 'admincp');
require_once './core/init.inc.php';
$navtitle.= ' - 管理员信息';

if($_GET['action'] != 'login' && !$_G['admin']->isLogged()){
	showmsg('您未处于登录状态。', 'index.php');
}

switch($_GET['action']){
case 'login':
	if($_G['admin']->login($_POST['username'], $_POST['password'], $_POST['method'])){
		showmsg('您已经成功登录！', 'refresh');
	}else{
		showmsg('登录错误！您的账号或者密码不正确！', 'back');
	}
break;

case 'logout':
	$_G['admin']->logout();
	showmsg('您已经成功退出！', 'index.php');
break;

case 'edit':
	if($mem = $_POST['admin']){
		$extmsg = '';
	
		if(!empty($mem['email']) && strpos($mem['email'], '@') === false){
			showmsg('电子邮件格式错误，请重新填写。', 'back');
		}

		$mem['gender'] = intval($mem['gender']);
		$mem['gender'] = $mem['gender'] >= 0 && $mem['gender'] <= 2 ? $mem['gender'] : 0;

		foreach(array('email', 'gender', 'avatar') as $item){
			$_G['admin']->v[$item] = $mem[$item];
		}
		
		if($_FILES['avatar_upload'] && $_FILES['avatar_upload']['name']){
			if(!$_G['admin']->uploadAvatar($_FILES['avatar_upload'])){
				$extmsg.= '头像上传失败！';
			}
		}
		
		if($mem['newpw']){
			if(!$mem['oldpw']){
				showmsg('您试图修改密码失败。请务必同时填写原始密码。', 'back');
			}
			
			$result = $_G['admin']->changePassword($mem['oldpw'], $mem['newpw'], $mem['newpw2']);
			if($result === -1){
				showmsg('抱歉，您输入的原密码错误。修改密码失败。', 'back');
			}else if($result === -2){
				showmsg('抱歉，您两次输入的新密码不一致，请重新输入。', 'back');
			}
		}
		
		showmsg('修改完成！'.$extmsg, 'refresh');

	}else{
		include view('memcp_edit');
	}
	break;

default:
	include view('memcp_edit');
}

?>